<?php

namespace App\Http\Controllers;

use App\Services\SocialTwitterAccountService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Facades\Socialite;
use Omnipay\Omnipay;

class TestController extends Controller
{
    public function wxpayNotify(Request $request)
    {
//        $data = $request->all();
        $data = file_get_contents("php://input");
        $gateway = Omnipay::create('WechatPay_Native');
        $gateway->setAppId(env('WECHAT_APPID'));
        $gateway->setMchId(env('WECHAT_MCH_ID'));
        $gateway->setApiKey(env('WECHAT_API_KEY'));


//        $data = $this->xmlToArray(file_get_contents('php://input'));
        $response = $gateway->completePurchase([
            'request_params' => file_get_contents('php://input')
        ])->send();

        $u = $response->isPaid();
//        file_put_contents('./1.log', 'wechat_bef ------ ' . PHP_EOL . json_encode($data) . 'wechat_bef ------ end' . PHP_EOL, FILE_APPEND);
        if($u){
            $post = $response->getRequestData();
            file_put_contents('./1.log', 'wechat_detail wechat ------ ' . PHP_EOL . json_encode($post) .PHP_EOL. 'wechat_detail ------ end' . PHP_EOL, FILE_APPEND);
            return $this->ret_result(true);
        }
        return $this->ret_result(false);
//        file_put_contents('./1.log',"php://input".PHP_EOL,FILE_APPEND);
        //wxpay_notify
//        file_put_contents('./1.log', 'wechat ------ ' . PHP_EOL . $data . PHP_EOL . 'wechat ------ end', FILE_APPEND);
        dd('wxpayNotify');
    }

    //支付宝同步回调
    public function aliPayReturn(Request $request)
    {
//        $data = $request->all();
        $data = file_get_contents("php://input");
        file_put_contents('./1.log', 'aliPayReturn ------ ' . PHP_EOL . json_encode($data) . PHP_EOL . 'aliPayReturn ------ end' . PHP_EOL, FILE_APPEND);
        return 'success';
        dd('aliPayReturn');
    }

    //支付宝异步回调
    public function postTest(Request $request)
    {
        $data = $request->all();
//        $data = file_get_contents("php://input");
        file_put_contents('./1.log', 'aliPayNotify ------ ' . PHP_EOL . json_encode($data) . PHP_EOL . 'aliPayNotify ------ end' . PHP_EOL, FILE_APPEND);
        return 'success';
        dd('postTest');
    }

    /**
     *  作用：将xml转为array
     */
    public function xmlToArray($xml)
    {
        //将XML转为array
        $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $array_data;
    }

    /**
     *  作用：array转xml
     */
    function arrayToXml($arr)
    {
        $xml = "<xml>";
        foreach ($arr as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";

            } else
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
        }
        $xml .= "</xml>";
        return $xml;
    }


    /**
     * 回调校验信息返回给支付网关
     */
    public function ret_result($is_success, $msg = '')
    {
        if ($is_success) {
            $ret = array('return_code' => 'SUCCESS', 'return_msg' => 'OK');
        } else {
            $ret = array('return_code' => 'FAIL', 'return_msg' => $msg);
        }
        $ret = $this->arrayToXml($ret);
        return $ret;
    }

    public function lijiaNotify(Request $request)
    {
        $u = file_get_contents('php://input');
        \Log::info('lijia',[$u]);
        $post = $request->all();
        \Log::info('lijia_notify',[$post]);
    }

}
